<?php
/**
 */
class My_taxApp extends BackendApp
{
    var $_my_tax_mod;

    function __construct()
    {
        $this->StockApp();
    }
    function StockApp()
    {
        parent::BackendApp();

        $this->_my_tax_mod =& m('my_tax');
    }

    /* 税率列表 */
    function index()
    {
        $tax_list = $this->_my_tax_mod->find(array('conditions' => '',));
        $this->assign('tax_list', $tax_list);
        $this->display('my_tax.index.html');
        $this->import_resource(array('script' => 'mlselection.js,inline_edit.js'));
    }

    function edit()
    {
        $id = empty($_GET['id']) ? 0 : intval($_GET['id']);
        if (!IS_POST)
        {
            /* 是否存在 */
            $tax = $this->_my_tax_mod->get_info($id);
            if (!$tax)
            {
                $this->show_warning('store_empty');
                return;
            }

            $this->assign('tax', $tax);

            $this->assign('states', array(
                STORE_OPEN   => Lang::get('open'),
                STORE_CLOSED => Lang::get('close'),
            ));

            $this->assign('recommended_options', array(
                '1' => Lang::get('yes'),
                '0' => Lang::get('no'),
            ));

            /* 导入jQuery的表单验证插件 */
            $this->import_resource(array(
                'script' => 'jquery.plugins/jquery.validate.js,mlselection.js'
            ));
            $this->assign('enabled_subdomain', ENABLED_SUBDOMAIN);
            $this->display('my_tax.form.html');
        }
        else
        {
            $store_info = $this->_my_tax_mod->get_info($id);
            $domain = empty($_POST['domain']) ? '' : trim($_POST['domain']);
            if ($domain && $domain != $store_info['domain'])
            {
                if (!$this->_my_tax_mod->check_domain($domain, Conf::get('subdomain_reserved'), Conf::get('subdomain_length')))
                {
                    $this->show_warning($this->_my_tax_mod->get_error());

                    return;
                }
            }
            $consolidated_tax = $this->get_consolidated_tax($_POST['consumption_tax'],$_POST['add_tax']);
            $data = array(
                'tax_name'   => $_POST['tax_name'],
                'consumption_tax'   => $_POST['consumption_tax'],
                'add_tax'   => $_POST['add_tax'],
                'consolidated_tax' => $consolidated_tax
            );
            /* 保存 */
            $rows = $this->_my_tax_mod->edit($id, $data);
            if ($this->_my_tax_mod->has_error())
            {
                $this->show_warning($this->_my_tax_mod->get_error());
                return;
            }else{
                $this->admin_log(Lang::get('edit_region'),1);
            }
            $ret_page = isset($_GET['ret_page']) ? intval($_GET['ret_page']) : 1;
            $this->show_message('edit_ok',
                'back_list',    'index.php?app=my_tax&page=' . $ret_page,
                'edit_again',   'index.php?app=my_tax&amp;act=edit&amp;id=' . $id
            );
        }
    }

    //计算综合税
    function get_consolidated_tax($consumption_tax = 0,$add_tax = 0.13){
        return round((($consumption_tax + $add_tax) / (1 - $consumption_tax)) * 0.7,3);
    }



    //异步修改数据
    function ajax_col()
    {
        $id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
        $column = empty($_GET['column']) ? '' : trim($_GET['column']);
        $value  = isset($_GET['value']) ? trim($_GET['value']) : '';
        $data   = array();

        if (in_array($column ,array('pro_recommended','pro_name')))
        {
            $data[$column] = $value;
            $this->_promotion_mod->edit($id, $data);
            if(!$this->_promotion_mod->has_error())
            {
                echo ecm_json_encode(true);
            }
        }
        else
        {
            return ;
        }
        return ;
    }

    /* 删除商品 */
    function drop() {
        $id = isset($_GET['id']) ? trim($_GET['id']) : '';
        if (!$id) {
            $this->show_warning('Hacking Attempt');
            return;
        }
        $ids = explode(',', $id);

        if (!$this->_my_tax_mod->drop($ids)) {
            $this->show_warning($this->_my_tax_mod->get_error());
            return;
        }else{
            $this->admin_log(Lang::get('del_promotion'),1);
        }
        $ret_page = isset($_GET['ret_page']) ? intval($_GET['ret_page']) : 1;
        $this->show_message('drop_ok', 'back_list', 'index.php?app=my_tax&page=' . $ret_page);
    }

    function  add(){

        if (!IS_POST)
        {
            /* 参数 */
            $this->display('my_tax.form.html');
        }
        else
        {
            $consolidated_tax = $this->get_consolidated_tax($_POST['consumption_tax'],$_POST['add_tax']);
            $data = array(
                'tax_name'   => $_POST['tax_name'],
                'consumption_tax'   => $_POST['consumption_tax'],
                'add_tax'   => $_POST['add_tax'],
                'consolidated_tax' => $consolidated_tax
            );

            /* 保存 */
            $region_id = $this->_my_tax_mod->add($data);
            if (!$region_id)
            {
                $this->show_warning($this->_my_tax_mod->get_error());
                return;
            }else{
                $this->admin_log(Lang::get('add_refgion'),1);
            }

            $this->show_message('add_ok',
                'back_list',    'index.php?app=region',
                'continue_add', 'index.php?app=region&amp;act=add&amp;pid=' . $data['parent_id']
            );
        }

    }
}

?>
